unit EditRakeValues;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, umainutils, Oracle, uSQLs, uUtils, gnugettext;

type
  TuFrmEditRakeValues = class(TForm)
    edt_FromVal: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    edt_ToVal: TLabeledEdit;
    edt_Value: TLabeledEdit;
    chb_IsPercent: TCheckBox;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure chb_IsPercentClick(Sender: TObject);
  private
    { Private declarations }
    m_IsClone, m_IsEdit: boolean;
    m_recordID: string;
  public
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string);
    { Public declarations }
  end;

var
  uFrmEditRakeValues: TuFrmEditRakeValues;

implementation

uses UfrmMain;
{$R *.dfm}
{ TuFrmEditCountries }

procedure TuFrmEditRakeValues.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditRakeValues.btn_SaveClick(Sender: TObject);
begin
  if m_IsClone or (not m_IsEdit) then
  begin
    InsertDataIntoDB('RAKE_VALUES', 'RAKE_ID,FROM_VALUE, TO_VALUE, VALUE,IS_PERCENT', '''' + FrmMain.stg_Rakes.Cells[0,FrmMain.stg_Rakes.Row] + ''''+','+'''' + edt_FromVal.Text + ''''+','+'''' + edt_ToVal.Text + ''''+','+'''' + edt_Value.Text + ''''+','+'''' + BtoStr(chb_IsPercent.Checked) + '''');
  end
  else
  begin
    UpdateDataToDB('RAKE_VALUES', 'RAKE_ID=''' + FrmMain.stg_Rakes.Cells[0,FrmMain.stg_Rakes.Row]+''',FROM_VALUE=''' + edt_FromVal.Text + ''',TO_VALUE=''' + edt_ToVal.Text + ''',VALUE=''' + edt_Value.Text + ''',IS_PERCENT=''' + BtoStr(chb_IsPercent.Checked) + '''', 'ID=' + m_recordID);
  end;
  FrmMain.GetGridData(FrmMain.stg_RakeValues, SQL_RakeValues, false);
  FrmMain.stg_RakesSelectCell(self, 0, FrmMain.stg_Rakes.Row, canselect);
  close();
end;

procedure TuFrmEditRakeValues.chb_IsPercentClick(Sender: TObject);
begin
  if chb_IsPercent.Checked then
  begin
    chb_IsPercent.Caption := '%';
  end
  else
  begin
    chb_IsPercent.Caption := umainutils.Currency_Symbol;
  end;
end;

procedure TuFrmEditRakeValues.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditRakeValues.ShowForm(IsEdit, IsClone: boolean; recordID: string);
var
  Query: string;
begin
  m_IsClone := IsClone;
  m_IsEdit := IsEdit;
  m_recordID := recordID;
  OracleQuery1.Session := FrmMain.OracleSession1;
  edt_FromVal.Text := '';
  edt_ToVal.Text := '';
  edt_Value.Text := '';
  if IsEdit then
  begin
    Query := 'select * from RAKE_VALUES where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    edt_FromVal.Text := OracleQuery1.Field('FROM_VALUE');
    edt_ToVal.Text := OracleQuery1.Field('TO_VALUE');
    edt_Value.Text := OracleQuery1.Field('VALUE');
    chb_IsPercent.Checked := StrToB(OracleQuery1.Field('IS_PERCENT'));
  end;
  ShowModal();
end;

end.
